<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
        <el-form-item label="退款编号" prop="sn">
          <el-input v-model="formData.sn" clearable placeholder="请输入退款编号" />
        </el-form-item>
        <el-form-item label="关联用户" prop="user_id">
          <el-input v-model="formData.user_id" clearable placeholder="请输入关联用户" />
        </el-form-item>
        <el-form-item label="来源订单id" prop="order_id">
          <el-input v-model="formData.order_id" clearable placeholder="请输入来源订单id" />
        </el-form-item>
        <el-form-item label="来源单号" prop="order_sn">
          <el-input v-model="formData.order_sn" clearable placeholder="请输入来源单号" />
        </el-form-item>
        <el-form-item label="订单来源 order-商品订单 recharge-充值订单" prop="order_type">
          <el-input v-model="formData.order_type" clearable placeholder="请输入订单来源 order-商品订单 recharge-充值订单" />
        </el-form-item>
        <el-form-item label="订单总的应付款金额，冗余字段" prop="order_amount">
          <el-input v-model="formData.order_amount" clearable placeholder="请输入订单总的应付款金额，冗余字段" />
        </el-form-item>
        <el-form-item label="本次退款金额" prop="refund_amount">
          <el-input v-model="formData.refund_amount" clearable placeholder="请输入本次退款金额" />
        </el-form-item>
        <el-form-item label="第三方平台交易流水号" prop="transaction_id">
          <el-input v-model="formData.transaction_id" clearable placeholder="请输入第三方平台交易流水号" />
        </el-form-item>
        <el-form-item label="退款方式 1-线上退款 2-线下退款" prop="refund_way">
          <el-input v-model="formData.refund_way" clearable placeholder="请输入退款方式 1-线上退款 2-线下退款" />
        </el-form-item>
        <el-form-item label="退款类型 1-后台退款" prop="refund_type">
          <el-input v-model="formData.refund_type" clearable placeholder="请输入退款类型 1-后台退款" />
        </el-form-item>
        <el-form-item label="退款状态，0退款中，1退款成功，2退款失败" prop="refund_status">
          <el-input v-model="formData.refund_status" clearable placeholder="请输入退款状态，0退款中，1退款成功，2退款失败" />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="orderRefundRecordEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiOrderRefundRecordAdd, apiOrderRefundRecordEdit, apiOrderRefundRecordDetail } from '@/api/order_refund_record'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑订单退款审核' : '新增订单退款审核'
})

// 表单数据
const formData = reactive({
  id: '',
  sn: '',
  user_id: '',
  order_id: '',
  order_sn: '',
  order_type: '',
  order_amount: '',
  refund_amount: '',
  transaction_id: '',
  refund_way: '',
  refund_type: '',
  refund_status: '',
})


// 表单验证
const formRules = reactive<any>({
  sn: [{
    required: true,
    message: '请输入退款编号',
    trigger: ['blur']
  }],
  user_id: [{
    required: true,
    message: '请输入关联用户',
    trigger: ['blur']
  }],
  order_id: [{
    required: true,
    message: '请输入来源订单id',
    trigger: ['blur']
  }],
  order_sn: [{
    required: true,
    message: '请输入来源单号',
    trigger: ['blur']
  }],
  order_amount: [{
    required: true,
    message: '请输入订单总的应付款金额，冗余字段',
    trigger: ['blur']
  }],
  refund_amount: [{
    required: true,
    message: '请输入本次退款金额',
    trigger: ['blur']
  }],
  refund_way: [{
    required: true,
    message: '请输入退款方式 1-线上退款 2-线下退款',
    trigger: ['blur']
  }],
  refund_type: [{
    required: true,
    message: '请输入退款类型 1-后台退款',
    trigger: ['blur']
  }],
  refund_status: [{
    required: true,
    message: '请输入退款状态，0退款中，1退款成功，2退款失败',
    trigger: ['blur']
  }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }


}

const getDetail = async (row: Record<string, any>) => {
  const data = await apiOrderRefundRecordDetail({
    id: row.id
  })
  setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate()
  const data = { ...formData,  }
  mode.value == 'edit'
      ? await apiOrderRefundRecordEdit(data)
      : await apiOrderRefundRecordAdd(data)
  popupRef.value?.close()
  emit('success')
}

//打开弹窗
const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
  emit('close')
}



defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
